package com.chinadci.smartcommunitysystembackstage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinadci.smartcommunitysystembackstage.entity.ScsPhone;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsAdmintypeMapper;
import com.chinadci.smartcommunitysystembackstage.mapper.ScsPhoneMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@Service
public class ScsPhoneService{
    @Autowired
    private ScsPhoneMapper mapper;

    private Logger log= LoggerFactory.getLogger(this.getClass());

    //查找全部便民电话
    public List<ScsPhone> Find(String name){
        log.info("=====查找全部便民电话");
        List<ScsPhone> list=null;
        if (name!="" & name!=null){
            list= mapper.Find("%"+name+"%");
        }else{
            list= mapper.Find("%%");
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setZw(list.get(i).getType().get(0).getPtPosition());
        }
        return list;
    }

    //新增便民电话
    public int Insert(ScsPhone scs){
        log.info("=====添加便民电话");
        return mapper.insert(scs);
    }

    //新增的时候查询一个电话号码只能有一个
    public ScsPhone Finds(String phone){
        log.info("=====新增的时候查询一个电话号码只能有一个");
        QueryWrapper<ScsPhone> qw=new QueryWrapper<>();
        qw.eq("p_phone",phone);
        return mapper.selectOne(qw);
    }

    //删除便民电话
    public int Del(int id){
        log.info("=====删除便民电话");
        QueryWrapper<ScsPhone> qw=new QueryWrapper<>();
        qw.eq("p_id",id);
        return mapper.delete(qw);
    }

    //修改便民电话
    public int Update(ScsPhone scs){
        log.info("=====修改便民电话");
        return mapper.updateById(scs);
    }

    //修改的时候查找
    public List<ScsPhone> Find(int id){
        log.info("=====修改的时候查找");
        List<ScsPhone> list=mapper.Finds(id);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setZw(list.get(i).getType().get(0).getPtPosition());
        }
        return list;
    }
}
